package com.itheima.reggie.service.impl;

import com.itheima.reggie.dao.UserDao;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public User login(String phone, String code, String verifyCode) {
        User user = null;
        // 1.校验验证码是否正确
        if(code.equalsIgnoreCase(verifyCode)){
            // 2.如果验证码没有问题，校验手机号是否已经是注册的，如果已经注册直接登录成功
            user = userDao.findByPhone(phone);
            // 3.如果没有注册，那么帮助该用户去注册
            if (user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userDao.save(user);
            }
        }
        return user;
    }
}
